/**
 * Created by WLG on 2018/11/12.
 */
let BaseService = require("./baseService");
let SystemConst = require("../config/systemConst");
let ChatService = require("../services/chatService");
let QueryData = require("../model/sysQueryData");
let OnlineService = require('../services/onlineService');
let ServerConfig = require('../config/serverConfig');
let CommontUtil = require('../lib/commonUtil');
/**
 * 消息推送服务
 */
class SocketPushService extends BaseService{
    static async pushChatMsg(io,chatMsg){
        let chat = await ChatService.getCache(chatMsg.chatID);
        let sendArray = chat.mem;
        let qd = new QueryData();
        qd.data = chatMsg;
        sendArray.forEach(async item =>{
            let memOnlineInfo = await OnlineService.getMemOnlineInfoCache(item);
            if(memOnlineInfo){
                let socketInfoMap = memOnlineInfo.socketMap;
                for(let tempSocket in socketInfoMap){
                    super.emitMsg(io,tempSocket,SystemConst.EventName.ChatMsgSocket.ServerSend,qd);
                }
                commonUtil.consoleTestInfo('Send','ChatMsg','To',memOnlineInfo.memInfo.name,'title',chat.title);
            }
        });
    }
    static async pushCloseMsgAndClose(io,socketId){
        let qd = new QueryData();
        qd.data = SystemConst.SocketMsg.SocketCloseOtherTerminalLogin;
        let tempSocket = io.of(ServerConfig.socketNameSpace).sockets[socketId];
        await tempSocket.emit(SystemConst.EventName.SystemSocket.ServerCloseForOtherLogin,qd);
        tempSocket.disconnect(true)
    }
    static async pushWithdrawMsg(io,chatID,chatMsgID,lastChatMsg){
        let chat = await ChatService.getCache(chatID,false);
        let memArray = chat.mem;
        let qd = new QueryData();
        qd.data = {
            chatID:chatID,
            chatMsgID:chatMsgID,
            lastChatMsg:lastChatMsg
        };
        memArray.forEach(async item =>{
            let memOnlineInfo = await OnlineService.getMemOnlineInfoCache(item);
            if(memOnlineInfo){
                let socketInfoMap = memOnlineInfo.socketMap;
                for(let tempSocket in socketInfoMap){
                    super.emitMsg(io,tempSocket,SystemConst.EventName.ChatMsgSocket.ServerWithdraw,qd);
                }
                commonUtil.consoleTestInfo('Send','WithdrawMsg','To',memOnlineInfo.memInfo.name,'title',chat.title);
            }
        });
    }
}
module.exports = SocketPushService;